/**
 * 
 */
package br.odb.bzk3.android.storytelling;

import java.io.InputStream;
import java.util.ArrayList;

import br.odb.bzk3.android.R;
import br.odb.bzk3.android.R.raw;
import bzk3.geometry.GLES1Triangle;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.View;

/**
 * @author monty
 * 
 */
public class SVGViewer extends View {

	private ArrayList<GLES1Triangle> shapes;

	public SVGViewer(Context context) {
		super(context);
	}

	public void init() {
		shapes = new ArrayList<GLES1Triangle>();

		try {
			InputStream is = getResources().openRawResource(R.raw.intro);
			int entries = is.read();
			byte[] bytes = new byte[10];
			GLES1Triangle t;
			for (int c = 0; c < entries; ++c) {

				is.read(bytes);

				t = new GLES1Triangle();
				t.a = (bytes[0] + (-Byte.MIN_VALUE));
				t.r = (bytes[1] + (-Byte.MIN_VALUE));
				t.g = (bytes[2] + (-Byte.MIN_VALUE));
				t.b = (bytes[3] + (-Byte.MIN_VALUE));

				t.x0 = ((bytes[4] + (-Byte.MIN_VALUE)) * getWidth()) / 255;
				t.y0 = ((bytes[5] + (-Byte.MIN_VALUE)) * getHeight()) / 255;

				t.x1 = ((bytes[6] + (-Byte.MIN_VALUE)) * getWidth()) / 255;
				t.y1 = ((bytes[7] + (-Byte.MIN_VALUE)) * getHeight()) / 255;

				t.x2 = ((bytes[8] + (-Byte.MIN_VALUE)) * getWidth()) / 255;
				t.y2 = ((bytes[9] + (-Byte.MIN_VALUE)) * getHeight()) / 255;

				shapes.add(t);
			}
		} catch (Exception e) {

		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		setBackgroundColor(Color.WHITE);
		if (shapes == null)
			init();

		Paint paint = new Paint();
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setColor(Color.BLACK);
		paint.setStrokeWidth(2);
		Path path = new Path();
		GLES1Triangle t;

		for (int c = 0; c < shapes.size(); ++c) {

			t = shapes.get(c);

			path = new Path();
			path.moveTo(t.x0, t.y0);
			path.lineTo(t.x1, t.y1);
			path.lineTo(t.x2, t.y2);
			path.close();

			paint.setColor(Color.rgb((int) t.r, (int) t.g, (int) t.b));
			paint.setAlpha((int) t.a);

			canvas.drawPath(path, paint);
		}
	}

}
